{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "\n",
    "#############################################\n",
    "frameWidth = 640\n",
    "frameHeight = 480\n",
    "nPlateCascade = cv2.CascadeClassifier(\"../tutorial/resources/haarcascade_russian_plate_number.xml\")\n",
    "minArea = 200\n",
    "color = (255,0,255)\n",
    "###############################################\n",
    "cap = cv2.VideoCapture(0)\n",
    "cap.set(3, frameWidth)\n",
    "cap.set(4, frameHeight)\n",
    "cap.set(10,150)\n",
    "count = 0\n",
    "\n",
    "while True:\n",
    "    success, img = cap.read()\n",
    "    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "    numberPlates = nPlateCascade.detectMultiScale(imgGray, 1.1, 10)\n",
    "    for (x, y, w, h) in numberPlates:\n",
    "        area = w*h\n",
    "        if area >minArea:\n",
    "            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 2)\n",
    "            cv2.putText(img,\"Number Plate\",(x,y-5),\n",
    "                        cv2.FONT_HERSHEY_COMPLEX_SMALL,1,color,2)\n",
    "            imgRoi = img[y:y+h,x:x+w]\n",
    "            cv2.imshow(\"ROI\", imgRoi)\n",
    "\n",
    "    cv2.imshow(\"Result\", img)\n",
    "\n",
    "    if cv2.waitKey(1) & 0xFF == ord('s'):\n",
    "        cv2.imwrite(\"../tutorial/resources/scanned/NoPlate_\"+str(count)+\".jpg\",imgRoi)\n",
    "        cv2.rectangle(img,(0,200),(640,300),(0,255,0),cv2.FILLED)\n",
    "        cv2.putText(img,\"Scan Saved\",(150,265),cv2.FONT_HERSHEY_DUPLEX,\n",
    "                    2,(0,0,255),2)\n",
    "        cv2.imshow(\"Result\",img)\n",
    "        cv2.waitKey(500)\n",
    "        count +=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
